package com.wanwu.channel.remote.apollo;

import com.alibaba.fastjson.JSON;
import com.ctrip.framework.apollo.ConfigService;
import com.wanwu.channel.common.util.LogUtil;
import com.wanwu.channel.common.vo.MqDelayVo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author runquanchen
 * @version 1.0
 * @date 2020-05-14 15:22
 * @project channel
 */
public class ApolloUtil {
    private static final Logger logger = LoggerFactory.getLogger(ApolloUtil.class);

    public static final String mqClearApply="mq.channel.clearapply";
    public static final String mqClearExec="mq.channel.clearexec";
    public static final String mqAliDeclare="mq.channel.alideclare";
    public static final String mqClearTrade="mq.channel.cleartrade";
    public static final String clearAccount="account.clear";

    private static final List<Integer> CLEAR_TRADE_QUEUE = Arrays.asList(300,300,300,300,300,300,300,300,300,300,300,300,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800);
    private static final List<Integer> AL_DECLARE_QUEUE = Arrays.asList(600,600,600,600,600,600,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800);
    private static final List<Integer> ABROADCLEAR_APPLY_QUEUE = Arrays.asList(1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800,10080,10080,10080,10080,10080,10080,10080,10080,10080,10080,10080,10080);
    private static final List<Integer> ABROADCLEAR_EXEC_QUEUE = Arrays.asList(300,300,300,300,300,300,300,300,300,300,300,300,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800);
    private static final List<Integer> DEFAULT_QUEUE = Arrays.asList(300,300,300,300,300,300,300,300,300,300,300,300,1800,1800,1800,1800,1800,1800,1800,1800,1800,1800);


    public static List<Integer> getDelayTimeList(String key){
        List<Integer> resultList = new ArrayList<>();

        String queue = ConfigService.getAppConfig().getProperty(key, "");


        MqDelayVo mqDelayVo = null;
        try {
            mqDelayVo = JSON.parseObject(queue,MqDelayVo.class);
        } catch ( Exception e) {
            LogUtil.error(logger,e, "获取配置异常，key为：" +key);
        }
        if (mqDelayVo == null || CollectionUtils.isEmpty(mqDelayVo.getFrequency())) {
            resultList = geDefaultDelay(key);
            LogUtil.error(logger, "获取配置异常，使用默认配置为：{0}",JSON.toJSONString(resultList));
            return resultList;
        }

        if (CollectionUtils.isNotEmpty(mqDelayVo.getSecond()) && mqDelayVo.getFrequency().size() ==mqDelayVo.getSecond().size() ) {
            for (int i = 0; i < mqDelayVo.getSecond().size(); i++) {
                for (int j = 0; j <mqDelayVo.getFrequency().get(i) ; j++) {
                    resultList.add(mqDelayVo.getSecond().get(i));
                }
            }
            LogUtil.info(logger, "获取apollo的mq配置正常,配置为：{0}",JSON.toJSONString(resultList));

            return resultList;
        }else if (CollectionUtils.isNotEmpty(mqDelayVo.getMinute()) && mqDelayVo.getFrequency().size() ==mqDelayVo.getMinute().size() ) {
            for (int i = 0; i < mqDelayVo.getMinute().size(); i++) {
                for (int j = 0; j <mqDelayVo.getFrequency().get(i) ; j++) {
                    resultList.add(mqDelayVo.getMinute().get(i)*60);
                }
            }
            LogUtil.info(logger, "获取apollo的mq配置正常,配置为：{0}",JSON.toJSONString(resultList));

            return resultList;
        }
        resultList = geDefaultDelay(key);

        LogUtil.error(logger, "配置错误，使用默认配置为：{0}",JSON.toJSONString(resultList));
        return resultList;

    }

    private static List<Integer> geDefaultDelay(String key) {
        if (mqClearApply.equals(key)) {
            return ABROADCLEAR_APPLY_QUEUE;
        } else if (mqClearExec.equals(key)) {
            return ABROADCLEAR_EXEC_QUEUE;
        }else if (mqAliDeclare.equals(key)) {
            return AL_DECLARE_QUEUE;
        } else if (mqClearTrade.equals(key)) {
            return CLEAR_TRADE_QUEUE;
        }else {
            return DEFAULT_QUEUE;
        }

    }

    public static Integer getDelayTime(List<Integer> list,int num){
        if (num>=list.size()) {
            return list.get(list.size()-1);
        }
        return list.get(num);
    }

    public static String getValue(String key){
        String keyValue = ConfigService.getAppConfig().getProperty(key, "");
        LogUtil.info(logger, "获取apollo的清分配置正常,配置为：{0}",keyValue);

        return keyValue;

    }

}
